package com.ymatou.iapi.settlement.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.settlement.parameter.SettleSubsidyBean;
import com.ymatou.iapi.settlement.service.SettleSubsidyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;

/**
 * 补贴结算接口 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_SettleSubsidy {
	private static SettleSubsidyBean settleSubsidyBean;
	private static SettleSubsidyCall settleSubsidyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("结算收入和扣佣接口");
	}

	@Before
	public void caseUp() {
		settleSubsidyBean = new SettleSubsidyBean();
		settleSubsidyCall = new SettleSubsidyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(settleSubsidyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 新交易下单 使用补贴商品
	 * 
	 * @return
	 */
	public static TradingServiceResp placeOrder() {

		PlaceOrderReq placeOrderReq = TradingCallService
				.buildStandPlaceOrderReq();
		
		OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(20336378, "sellerName");
		OrderVo orderVo = TradingCallService.createOrderVo();
		
		OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
				"31a36c3f-a353-4c9e-a746-3e2b3df4491b", "fd9867a0-9e72-492a-89b9-9543aaad1852");
		
		orderItemVo.setProductNum(1);
		orderItemVo.setActivity(TradingCallService.createActivityVo(27362));
		
		orderVo.setOrderItems(Arrays.asList(orderItemVo));
		orderGroupVo.setOrders(Arrays.asList(orderVo));
		placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));	

		return TradingCallService.callPlaceOrderService(placeOrderReq);
	}

	@SuppressWarnings("rawtypes")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSubsidy_001() {
		Logger.start(true, "补贴商品结算成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = placeOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			//支付
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			TradingCallService.delivery(sellerId, orderId, false);

			//确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Thread.sleep(5000);

			// 结算前账户金额
			BigDecimal beforeAmount = AccountCallServiceV2.getAccountBalance(
					(int) sellerId, 1);

			Logger.comment("前置数据准备结束");

			DefaultWapper defaultWapper = new DefaultWapper();

//			HashMap<String, Integer> m1 = new HashMap<String, Integer>();

			settleSubsidyBean.setOrderId((int) orderId);
			settleSubsidyCall.setData(settleSubsidyBean);
			settleSubsidyCall.callService();

			Logger.verifyEquals("ok", settleSubsidyCall.getReturnData(),
					"验证返回报文");

			//结算后账户金额
			BigDecimal afterAmount = AccountCallServiceV2.getAccountBalance(
					(int) sellerId, 1);
			// 补贴结算
			List<Map> subsidySettlementList = defaultWapper
					.selectSubsidySettlementInvoiceByOrderId(orderId);

			BigDecimal subsidyAmount = (BigDecimal) subsidySettlementList
					.get(0).get("SubsidyAmount");

			Logger.verifyEquals(subsidyAmount,
					afterAmount.subtract(beforeAmount), "验证商家账务余额变化");

			Logger.verifyEquals(
					1,
					(Integer) subsidySettlementList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");

			Logger.verifyEquals("订单补贴", (String) subsidySettlementList.get(0)
					.get("Remark"), "验证Remark");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@SuppressWarnings("rawtypes")
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSubsidy_002() {
		Logger.start(true, "补贴商品重复结算");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			TradingServiceResp placeOrderResp = placeOrder();

			Order order = placeOrderResp.getOrderList().get(0);

			long userId = placeOrderResp.getUserId();
			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			//支付
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			//接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			TradingCallService.delivery(sellerId, orderId, false);

			//确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Thread.sleep(5000);

			// 结算前账户金额
			BigDecimal beforeAmount = AccountCallServiceV2.getAccountBalance(
					(int) sellerId, 1);

			Logger.comment("前置数据准备结束");

			DefaultWapper defaultWapper = new DefaultWapper();

//			HashMap<String, Integer> m1 = new HashMap<String, Integer>();

			settleSubsidyBean.setOrderId((int) orderId);
			settleSubsidyCall.setData(settleSubsidyBean);
			settleSubsidyCall.callService();

			Logger.verifyEquals("ok", settleSubsidyCall.getReturnData(),
					"验证返回报文");
			Logger.comment("重复补贴结算");

			settleSubsidyCall.callService();

			//结算后账户金额
			BigDecimal afterAmount = AccountCallServiceV2.getAccountBalance(
					(int) sellerId, 1);
			// 补贴结算
			List<Map> subsidySettlementList = defaultWapper
					.selectSubsidySettlementInvoiceByOrderId(orderId);

			BigDecimal subsidyAmount = (BigDecimal) subsidySettlementList
					.get(0).get("SubsidyAmount");

			Logger.verifyEquals(subsidyAmount,
					afterAmount.subtract(beforeAmount), "验证商家账务余额变化");

			Logger.verifyEquals(
					1,
					(Integer) subsidySettlementList.get(0).get(
							"SettlementStatus"), "验证SettlementStatus");

			Logger.verifyEquals("订单补贴", (String) subsidySettlementList.get(0)
					.get("Remark"), "验证Remark");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SettleSubsidy_003() {
		Logger.start(false, "订单号为null");
		try {

			// settleSubsidyBean.setOrderId(orderId);
			settleSubsidyCall.setData(settleSubsidyBean);
			settleSubsidyCall.callService();
			Logger.comment("重复结算");
			settleSubsidyCall.callService();
			Logger.verifyEquals("ok", settleSubsidyCall.getReturnData(),
					"验证返回报文");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
